<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="dao.QueryStudents"%>
<%@page import="dao.Entity.Students"%>
<%@page import="dao.Entity.Student"%>
<%@page import="java.util.ArrayList"%>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link href="css/allcss.css" rel="stylesheet" type="text/css"/>
        <link rel="shortcut icon"  href="image/icon6.ico" type="image/x-icon" /> 
        <script src="./script/jquery.js" type="text/javascript"></script>
        <style>
            #op7{
                color:#00aeec;
            }    
        </style>
        <title>Students</title>
    </head>
    <body>
        <div id="aside">
            <img id="icon" src="./image/icon6.png">
            <div id="p1">管理员界面</div>    
        </div>
        <div id="main">
            <div id="list"><div id="op1" class="options">全部预约</div><div  id="op2" class="options">未审核</div>
                <div  id="op3" class="options">已审核</div><div  id="op4" class="options">地点管理</div>
                <div  id="op5" class="options">地点上新</div><div  id="op6" class="options">数据库管理</div>
                <div  id="op7" class="options">学生信用度</div><div  id="op8" class="options">申诉回复</div>
            </div>
            <div id="content">
                <%
                    ArrayList<Student> students = QueryStudents.getResult().students;
                    for (int i = 0; i < students.size(); i++) {
                        String rate = null;
                        double color = 0;
                        int C16 = 255;
                        if (students.get(i).getBorrowCount() == 0) {
                            rate = "~";
                        } else {
                            color = 1 - students.get(i).getDefaults() * 1.0 / students.get(i).getBorrowCount();
                            C16 = (int) (color * 255);
                            rate = String.format("%.2f", color * 100);
                        }
                        String status = (students.get(i).getBorrowStatus() == 1 ? "green" : "red");
                        out.println("<div  class='students'><div class='student' >学生号：[" + students.get(i).getStudentAccount()
                                + "] 借乐器次数：[" + students.get(i).getBorrowCount() + "次] 违约次数：[" + students.get(i).getDefaults() + "] </div><div class='credit' style='color:rgb(" + (255 - C16) + "," + C16 + ",0);'>信用度:[" + rate + "%]</div>"
                                + "<img id='img" + students.get(i).getStudentAccount() + "' class='students_status_img' src='./image/" + status + ".png'/><button class='students_open_button' name='" + students.get(i).getStudentAccount() + "'>允许借用</button><button class='students_stop_button' name='" + students.get(i).getStudentAccount() + "'>禁止借用</button></div>");
                    }
                %>
            </div>
        </div> 
        <script src="./script/button.js" type="text/javascript"></script>
        <script>
            $(".students_open_button").click(function () {
                var name = $(this).attr('name');
                $.get("./Servlet/changeStudentStatusServlet?type=1&StudentAccount=" + name, function (data, status) {
                    if (status) {
                        if (data !== "1") {
                            alert("失败");
                        } else {
                            alert("成功");
                            $("#img" + name).attr("src", "./image/green.png");
                        }
                    }
                }
                );
            });
            $(".students_stop_button").click(function () {
                var name = $(this).attr('name');
                $.get("./Servlet/changeStudentStatusServlet?type=0&StudentAccount=" + name, function (data, status) {
                    if (status) {
                        if (data !== "1") {
                            alert("失败");
                        } else {
                            alert("成功");
                            $("#img" + name).attr("src", "./image/red.png");
                        }
                    }
                }
                );
            });
        </script>
    </body>
</html>